package ext.tianma.part.ecn;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import ext.com.iba.IBAUtil;
import ext.com.workflow.WorkflowUtil;
import ext.generic.part.PartCommands;
import ext.generic.util.Generic;
import ext.tianma.bom.bean.ECOBean;
import ext.tianma.bom.eco.BOMHelp;
import ext.tianma.bom.util.CSCIBA;
import ext.tianma.bom.util.GenericUtil;
import ext.tianma.bom.util.LWCUtil;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.exception.TmException;
import ext.tianma.part.CheckPartReveal;
import ext.tianma.part.PartMarkUtil;
import ext.tianma.part.PartWorkflowUtil;
import ext.tianma.part.forms.TMTypeUtil;
import ext.tianma.part.forms.TransitFactoryUtil;
import ext.tianma.util.IbaAttributeUtil;
import ext.tianma.util.PropertiesUtil;
import wt.change2.ChangeException2;
import wt.change2.ChangeHelper2;
import wt.change2.Changeable2;
import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeOrder2;
import wt.doc.WTDocument;
import wt.epm.EPMDocument;
import wt.fc.ObjectReference;
import wt.fc.Persistable;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.fc.collections.WTArrayList;
import wt.lifecycle.State;
import wt.log4j.LogR;
import wt.method.MethodContext;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.pom.WTConnection;
import wt.util.WTException;
import wt.workflow.engine.ProcessData;
import wt.workflow.engine.WfEngineHelper;
import wt.workflow.engine.WfProcess;
import wt.workflow.engine.WfState;

/**
 * 配套物料申请Util
 * 
 * @author gjw
 *
 */
public class MaterialsApplicationUtil {
	private static final Logger log = LogR.getLogger(MaterialsApplicationUtil.class.getName());
	private static ArrayList list = new ArrayList();
	public static String configFilePath = "codebase" + File.separator + "ext" + File.separator + "tianma"
			+ File.separator + "cfg" + File.separator + "TianmaPro.properties";
	private static WTArrayList objectList = new WTArrayList();
	private static List<String> stateList = new ArrayList<String>();

	private static final String CLASSNAME = MaterialsApplicationUtil.class.getName();

	// 物料生效地
	private static final String PART_NUMBER_EFFECTIVE_SITE = "partNumberEffectiveSite";

	private static final String D_VIEW = "Design";

	private static final String NEW_PRODUCT_MODIFY = "NewProductModify";
	private static final String MASS_PRODUCT_MODIFY = "MassProductionReleaseModify";
	private static final String INWORK = "INWORK";
	private static final String APPROVE_MODIFICATIONS = "APPROVE_MODIFICATIONS";
	private static final String NEW_PRODUCT = "NewProduct";
	private static final String MASS_RELEASE = "MassProductionRelease";
	private static final String APPROVEL = "APPROVAL";

	private static final String RELEASED = "RELEASED";

	// 试产
	private static final String TRIAL_PRODUCTION = "TrialProduction";
	// 小批量
	private static final String CUSTOMER_SAMPLE = "CustomerSample";
	// 风险批量
	private static final String RISK_ORDER = "RiskOrder";
	// 试产修改
	private static final String TRIAL_PRODUCTION_MODIFY = "TrialProductionModify";
	// 小批量修改
	private static final String CUSTOMER_SAMPLE_MODIFY = "CustomerSampleModify";
	// 风险批量修改
	private static final String RISK_ORDER_MODIFY = "RiskOrderModify";

	// OBSOLESCENCE 废弃
	private static final String OBSOLESCENCE = "OBSOLESCENCE";

	// NewPartModify 新物料修改
	private static final String NewPartModify = "NewPartModify";

	// cn.tianma.RawMaterial 原材料
	private static String softype_RawMaterial = "cn.tianma.RawMaterial";

	// 原材料 上偏光片 cn.tianma.TopPolarizer
	private static String softype_TopPolarizer = "cn.tianma.TopPolarizer";

	// 原材料 下偏光片 cn.tianma.BotPolarizer
	private static String softype_BotPolarizer = "cn.tianma.BotPolarizer";

	// 原材料 虚拟偏光片组件 cn.tianma.PhantomPolarizer
	private static String softype_PhantomPolarizer = "cn.tianma.PhantomPolarizer";

	// 原材料属性（是否已经样品承认）
	private static final String WHETHER_PART_RECOGNIZE = "whetherPartRecognize";

	private static String filePath = "/ext/tianma/cfg/TianmaPro.properties"; // 偏光片
	
	private static final String MaterialWorkflow = "配套物料申请流程-半成品";// 配套物料申请流程-半成品流程

	static {
		// 添加状态“新品修改”，“量产发布修改”，“正在工作”，“待批准”，“量产发布”，“新品”
		stateList.add("NewProductModify");
		stateList.add("MassProductionReleaseModify");
		stateList.add("INWORK");
		stateList.add("APPROVE_MODIFICATIONS");
		stateList.add("MassProductionRelease");
		stateList.add("NewProduct");
		stateList.add("APPROVAL");
		// 添加状态“试产”，“小批量”，“风险批量”，“试产修改”，“小批量修改”，“风险批量修改”
		stateList.add("TrialProduction");
		stateList.add("CustomerSample");
		stateList.add("RiskOrder");
		stateList.add("TrialProductionModify");
		stateList.add("CustomerSampleModify");
		stateList.add("RiskOrderModify");
	}

	/**
	 * 设置受影响对象状态 为废弃 并添加到集合中
	 * 
	 * @param wtObj
	 * @return
	 */
	public static WTArrayList setChangeableOBSOLESCENCEState(WTObject wtObj) {
		WTArrayList reviewObjs = new WTArrayList();

		if (wtObj != null && wtObj instanceof WTChangeOrder2) {
			WTChangeOrder2 ecn = (WTChangeOrder2) wtObj;

			try {
				QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(ecn);

				if (queryResultECA != null) {

					while (queryResultECA.hasMoreElements()) {

						WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA

						setChangeableState(eca, reviewObjs);
					}
				}
			} catch (ChangeException2 e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return reviewObjs;
	}

	/**
	 * 设置受影响对象状态 为废弃 并添加到集合中
	 * 
	 * @param wtObj
	 * @return
	 */
	public static WTArrayList setChangeableStateNewPartModify(WTObject wtObj) {
		WTArrayList reviewObjs = new WTArrayList();

		if (wtObj != null && wtObj instanceof WTChangeOrder2) {
			WTChangeOrder2 ecn = (WTChangeOrder2) wtObj;

			try {
				QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(ecn);

				if (queryResultECA != null) {

					while (queryResultECA.hasMoreElements()) {

						WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA

						setChangeableStateToNewPartModify(eca, reviewObjs);
					}
				}
			} catch (ChangeException2 e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return reviewObjs;
	}

	/**
	 * 设置受影响对象状态 并添加到集合中
	 * 
	 * @param eca
	 * @param reviewObjs
	 * @throws ChangeException2
	 * @throws WTException
	 */
	private static void setChangeableState(WTChangeActivity2 eca, WTArrayList reviewObjs)
			throws ChangeException2, WTException {

		if (eca != null) {
			// 受影响对象
			QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);

			if (queryResultPart != null) {

				while (queryResultPart.hasMoreElements()) {

					Changeable2 changeable = (Changeable2) queryResultPart.nextElement();

					if (changeable != null && changeable instanceof WTPart) {
						WTPart part = (WTPart) changeable;
						setPartsState(part, reviewObjs);
					}
				}
			}
		}
	}

	/**
	 * 设置受影响对象状态 为新物料修改 并添加到集合中
	 * 
	 * @param eca
	 * @param reviewObjs
	 * @throws ChangeException2
	 * @throws WTException
	 */
	private static void setChangeableStateToNewPartModify(WTChangeActivity2 eca, WTArrayList reviewObjs)
			throws ChangeException2, WTException {

		if (eca != null) {
			// 受影响对象
			QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);

			if (queryResultPart != null) {

				while (queryResultPart.hasMoreElements()) {

					Changeable2 changeable = (Changeable2) queryResultPart.nextElement();

					if (changeable != null && changeable instanceof WTPart) {
						WTPart part = (WTPart) changeable;
						setPartsStateNewPartModify(part, reviewObjs);
					}
				}
			}
		}
	}

	/**
	 * 设置受影响对象状态为废弃 并添加到集合中
	 * 
	 * @param part
	 * @param reviewObjs
	 * @throws WTException
	 */
	private static void setPartsState(WTPart part, WTArrayList reviewObjs) throws WTException {

		if (part != null) {
			State state = part.getLifeCycleState();

			if (state != null) {
				String partState = state.toString();

				if (partState != null) {

					if (!partState.equals(OBSOLESCENCE)) {

						part = (WTPart) ext.com.workflow.WorkflowUtil.setLifeCycleState(part, OBSOLESCENCE);
					}
					reviewObjs.add(part);
				}
			}
		}
	}

	/**
	 * 设置受影响对象状态为新物料修改 并添加到集合中
	 * 
	 * @param part
	 * @param reviewObjs
	 * @throws WTException
	 */
	private static void setPartsStateNewPartModify(WTPart part, WTArrayList reviewObjs) throws WTException {

		if (part != null) {
			State state = part.getLifeCycleState();

			if (state != null) {
				String partState = state.toString();

				if (partState != null) {

					if (!partState.equals(NewPartModify)) {

						part = (WTPart) ext.com.workflow.WorkflowUtil.setLifeCycleState(part, NewPartModify);
					}
					reviewObjs.add(part);
				}
			}
		}
	}

	/**
	 * 判断是否加签审核
	 * 
	 * @param obj
	 * @return
	 */
	public static String setcountersignAudit(WTPart part) {
		String countersignAudit = "";
		List<String> list = new ArrayList<String>();

		if (part != null && TMTypeUtil.isRawMaterial(part)) {
			try {
				String partCName = (String) IBAUtil.getIBAValue(part, "partCName");
				Vector partEffectiveSite = IBAUtil.getIBAValue2(part, "partNumberEffectiveSite");
				log.info("setcountersignAudit中文名称：" + partCName);
				log.info("setcountersignAudit方法料号生效地：" + partEffectiveSite);
				if (partCName != null && !"".equals(partCName)) {
					if (partEffectiveSite != null && !"".equals(partEffectiveSite)) {
						String effectiveSite = partEffectiveSite.toString();
						if (effectiveSite.contains("C01") || effectiveSite.contains("C02")
								|| effectiveSite.contains("C04") || effectiveSite.contains("C06")
								|| effectiveSite.contains("C08")) {
							String key = partCName + ".HeadAssessor";
							Object value = Generic.getStrFromProperties(key, filePath);
							log.info("key值：" + key);
							log.info("value值：" + value);
							if (value != null && "Y".equals(value.toString())) {
								countersignAudit = "Y";
							}
						}
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return countersignAudit;
	}

	/**
	 * 判断是否加签审核
	 * 
	 * @param wtObj
	 * @return
	 */
	public static String setcountersignAuditByECN(WTObject wtObj) {
		String countersignAudit = "";

		List<String> list = new ArrayList<String>();
		if (wtObj != null && wtObj instanceof WTChangeOrder2) {
			WTChangeOrder2 ecn = (WTChangeOrder2) wtObj;
			try {
				QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(ecn);
				if (queryResultECA != null) {
					while (queryResultECA.hasMoreElements()) {
						WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
						if (eca != null) {
							// 受影响对象
							QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
							if (queryResultPart != null) {
								while (queryResultPart.hasMoreElements()) {
									Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
									if (changeable != null && changeable instanceof WTPart) {
										WTPart part = (WTPart) changeable;
										countersignAudit = setcountersignAudit(part);
										list.add(countersignAudit);
									}
								}
							}
						}
					}
				}
				if (list.contains("Y")) {
					countersignAudit = "Y";
				}
			} catch (ChangeException2 e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return countersignAudit;
	}

	/*
	 * @description 深圳地生效的原材料申请流程中增加会签者
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setCountersignParticipantsByECN(Object obj, String role, WTChangeOrder2 eco) {
		String countersignAudit = "";
		try {
			QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;
									countersignAudit = setcountersignAudit(part);
									if (!isNullOrEmpty(countersignAudit) && countersignAudit.startsWith("Y")) {
										setCountersignParticipants(obj, "HeadAssessor", part);
									}

								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}

	}

	/*
	 * @description 深圳地生效的原材料申请流程中增加会签者
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setCountersignParticipants(Object obj, String role, WTPart part) {

		if (obj == null || role == null || part == null) {
			return;
		}
		try {
			String partCName = (String) IBAUtil.getIBAValue(part, "partCName");
			Vector effectiveSite = (Vector) IBAUtil.getIBAValue2(part, "partNumberEffectiveSite");
			log.info("effectiveSite = " + effectiveSite);
			String contact = "";
			if (effectiveSite != null && !"".equals(effectiveSite)) {
				contact = partCName + ".HeadAssessor";
			}
			// 读取配置文件的users
			Object userNames = Generic.getStrFromProperties(contact, filePath);
			log.info("setCountersignParticipants method userNames = " + userNames);
			if (userNames != null && StringUtils.isNotBlank(userNames.toString())) {
				String userName = (String) userNames;
				Vector<WTUser> userList = PartCommands.getUserList(userName);
				if(userList.size()!=0){
					log.info("setCountersignParticipants method userNameArray = " + userList.get(0).getName());
					boolean flag = WorkflowUtil.addPrincipalToProcessRole(obj, role, userList.get(0));
					log.info("setCountersignParticipants method flag = " + flag);
				}
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}

	/*
	 * @description 深圳地生效的原材料申请流程中增加会签者
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setBOMParticipantsByECN(Object obj, String role, WTChangeOrder2 eco) {
		String countersignAudit = "";
		try {
			QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;

									setBOMParticipants(obj, "BOMSigner", part);

								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}

	}

	/*
	 * @description 不同物料生效地的原材料申请流程中分配BOM会签者
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setBOMParticipants(Object obj, String role, WTPart part) {

		if (obj == null || role == null || part == null) {
			return;
		}
		try {
			Vector partNumberEffectiveSite = (Vector) IBAUtil.getIBAValue2(part, "partNumberEffectiveSite");
			Enumeration effectiveSiteEnum = partNumberEffectiveSite.elements();
			String effectiveSite = "";
			String contact = "";
			while (effectiveSiteEnum.hasMoreElements()) {
				effectiveSite = (String) effectiveSiteEnum.nextElement();
				log.info(effectiveSite + ",");
				if ((part != null) && (TMTypeUtil.isSemiFinishedGood(part))) {
					contact = effectiveSite + ".SemiFinishedGood" + ".BOMSigner";
				}
				if ((part != null) && (TMTypeUtil.isRawMaterial(part))) {
					contact = effectiveSite + ".RawMaterial" + ".BOMSigner";
				}
				// 读取配置文件的users
				Object userNames = Generic.getStrFromProperties(contact, filePath);
				log.info("setBOMParticipants method userNames = " + userNames);
				if (userNames != null && StringUtils.isNotBlank(userNames.toString())) {
					String userName = (String) userNames;
					Vector<WTUser> userList = PartCommands.getUserList(userName);
					log.info("setBOMParticipants method userNameArray = " + userList.get(0).getName());
					boolean flag = WorkflowUtil.addPrincipalToProcessRole(obj, role, userList.get(0));
					log.info("setBOMParticipants method flag = " + flag);
				}
			}

		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}

	public static boolean isNullOrEmpty(String str) {
		return (str == null || "".equals(str) || str.equals("null") || str.equals("NULL"));
	}

	/**
	 * 设置受影响对象状态 为正在工作 并添加到集合中
	 * 
	 * @param wtObj
	 * @return
	 */
	public static WTArrayList setChangeableStateInWork(WTObject wtObj) {
		WTArrayList reviewObjs = new WTArrayList();

		if (wtObj != null && wtObj instanceof WTChangeOrder2) {
			WTChangeOrder2 ecn = (WTChangeOrder2) wtObj;

			try {
				QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(ecn);

				if (queryResultECA != null) {

					while (queryResultECA.hasMoreElements()) {

						WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA

						setChangeableStateToInWork(eca, reviewObjs);
					}
				}
			} catch (ChangeException2 e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return reviewObjs;
	}

	/**
	 * 设置受影响对象状态 为正在工作 并添加到集合中
	 * 
	 * @param eca
	 * @param reviewObjs
	 * @throws ChangeException2
	 * @throws WTException
	 */
	private static void setChangeableStateToInWork(WTChangeActivity2 eca, WTArrayList reviewObjs)
			throws ChangeException2, WTException {

		if (eca != null) {
			// 受影响对象
			QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);

			if (queryResultPart != null) {

				while (queryResultPart.hasMoreElements()) {

					Changeable2 changeable = (Changeable2) queryResultPart.nextElement();

					if (changeable != null && changeable instanceof WTPart) {
						WTPart part = (WTPart) changeable;
						setPartsStateInWork(part, reviewObjs);
					}
				}
			}
		}
	}

	/**
	 * 设置受影响对象状态为正在工作 并添加到集合中
	 * 
	 * @param part
	 * @param reviewObjs
	 * @throws WTException
	 */
	private static void setPartsStateInWork(WTPart part, WTArrayList reviewObjs) throws WTException {

		if (part != null) {
			State state = part.getLifeCycleState();

			if (state != null) {
				String partState = state.toString();

				if (partState != null) {

					if (!partState.equals(INWORK)) {

						part = (WTPart) ext.com.workflow.WorkflowUtil.setLifeCycleState(part, INWORK);
					}
					reviewObjs.add(part);
				}
			}
		}
	}

	/**
	 * 通过设计视图版本的零部件和料号生效产地属性，新建多个视图版本
	 * 
	 * 主要用于料号申请流程走完后，按料号生效地产生对应产地的物料
	 * 
	 * @param pbo
	 */
	public static void createMultViewVersionByDPartByECN(WTChangeOrder2 eco) {
		try {
			QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;

									TransitFactoryUtil.createMultViewVersionByDPart(part);

								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据ECN 中受影响对象 获取“Desgin”视图对象的其他视图对象并添加到集合中
	 * 
	 * @param eco
	 * @return
	 */
	public static WTArrayList getAllPartViewsByECN(WTChangeOrder2 eco) {
		WTArrayList erpObjList = new WTArrayList();
		try {
			QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;
									// 获取“Desgin”视图对象的其他视图对象并添加到集合中
									erpObjList.addAll(PartWorkflowUtil.getAllPartViews(part));
								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}

		return erpObjList;

	}

	/**
	 * 根据ECN 中受影响对象 获取“Desgin”视图对象的其他视图对象并添加到集合中
	 * 
	 * @param eco
	 * @return
	 */
	public static String getEffectNumberByECN(WTChangeOrder2 eco) {
		String numbers = "";
		try {
			QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;

									String number = part.getNumber();
									numbers += number + ",";
								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		if (numbers.length() != 0 && numbers.lastIndexOf(",") == (numbers.length() - 1)) {
			numbers = numbers.substring(0, numbers.length() - 1);
		}
		return numbers;

	}

	/**
	 * 根据ECN 中受影响对象 设置Mark
	 * 
	 * @param eco
	 * @return
	 * @throws WTException
	 * @throws ChangeException2
	 * @throws RemoteException
	 * @throws TmException
	 */
	public static void setPartMarkByECN(WTChangeOrder2 eco, WTPart POLPart)
			throws ChangeException2, WTException, RemoteException, TmException {
		QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
		// key 类型 value 物料
		Map<String, WTPart> map = new HashMap<String, WTPart>();
		if (queryResultECA != null) {
			while (queryResultECA.hasMoreElements()) {
				WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
				if (eca != null) {
					// 受影响对象
					QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
					if (queryResultPart != null) {
						while (queryResultPart.hasMoreElements()) {
							Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
							if (changeable != null && changeable instanceof WTPart) {
								WTPart part = (WTPart) changeable;
								String typename = GenericUtil.getTypeName(part);
								
								map.put(typename, part);
								System.out.println("=======typename:"+typename);
								if ("上偏光片".equals(typename)) {
									PartMarkUtil.setPartMark(part, POLPart);
								} /*else {
									// 下偏光片时 根据上POL号 取上POL号的mark值更新下偏光片的mark值
									String topnumber = IbaAttributeUtil.getAttributeValue(part, "MtlNumofPOLTop");
									if (!StringUtil.isNullOrEmpty(topnumber)) {
										try {
											WTPart toppart = GenericUtil.getPart(topnumber, "", "Design");
											if (toppart != null) {
												PartMarkUtil.setPartMark(part, toppart);
											}
										} catch (Exception e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}

									}
								}*/
							}
						}
					}
				}
			}
		}
		// 更新下偏光片Mark值
		updatePartMark(map);

	}

	/**
	 * 根据ECN 中受影响对象 设置Mark
	 * 
	 * @param eco
	 * @return
	 * @throws WTException
	 * @throws ChangeException2
	 * @throws RemoteException
	 * @throws TmException
	 */
	public static void writeBackPOLNumByECN(WTChangeOrder2 eco, WTPart POLPart)
			throws ChangeException2, WTException, RemoteException, TmException {
		QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
		if (queryResultECA != null) {
			while (queryResultECA.hasMoreElements()) {
				WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
				if (eca != null) {
					// 受影响对象
					QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
					if (queryResultPart != null) {
						while (queryResultPart.hasMoreElements()) {
							Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
							if (changeable != null && changeable instanceof WTPart) {
								WTPart part = (WTPart) changeable;
								PartMarkUtil.writeBackPOLNum(part, POLPart);
							}
						}
					}
				}
			}
		}

	}

	/**
	 * 判断只有原材料以及偏光片走“法规审核节点”
	 * 
	 * @param obj
	 * @return
	 */
	public static String setLegislationAuditByECN(WTChangeOrder2 eco) {
		String iflegislationAudit = "";
		List<String> list = new ArrayList<String>();
		QueryResult queryResultECA;
		try {
			queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;
									// CheckPartReveal.setLegislationAudit(part);
									list.add(CheckPartReveal.setLegislationAudit(part));
								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (list.contains("Y")) {
				iflegislationAudit = "Y";
			}
		}
		return iflegislationAudit;
	}

	/**
	 * 虚拟偏光片不走“审核信息输入”节点 （二供 和 原材料）
	 * 
	 * @param obj
	 * @return
	 */
	public static String opinionObjSByECN(WTChangeOrder2 eco) {
		String iflegislationAudit = "";
		List<String> list = new ArrayList<String>();
		QueryResult queryResultECA;
		try {
			queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;
									// CheckPartReveal.opinionObjS(part);
									list.add(CheckPartReveal.opinionObjS(part));
								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (list.contains("Y")) {
				iflegislationAudit = "Y";
			}
		}
		return iflegislationAudit;
	}

	/**
	 * 判断是否走PM审核结点
	 * 
	 * @param obj
	 * @return
	 */
	public static String setPMAuditByECN(WTChangeOrder2 eco) {
		String iflegislationAudit = "";
		List<String> list = new ArrayList<String>();
		QueryResult queryResultECA;
		try {
			queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;
									// CheckPartReveal.setLegislationAudit(part);
									list.add(CheckPartReveal.setPMAudit(part));
								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (list.contains("Y")) {
				iflegislationAudit = "Y";
			}
		}
		return iflegislationAudit;
	}

	/**
	 * 原材料生成物料号
	 * 
	 * @param obj
	 * @return
	 */
	public static String generateRowMaterialNumberByECN(WTChangeOrder2 eco) {
		String iflegislationAudit = "";
		List<String> list = new ArrayList<String>();
		// key 类型 value 物料
		Map<String, WTPart> map = new HashMap<String, WTPart>();
		QueryResult queryResultECA;
		try {
			queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;
									// CheckPartReveal.setLegislationAudit(part);
									// ext.tianma.part.PartCommands.generateRowMaterialNumber(part);

									part = PartCommandsUtil.generateRowMaterialNumber(part);

									String typeid = GenericUtil.getTypeId(part);
									String typename = GenericUtil.getTypeName(part);
									map.put(typename, part);
								}
							}
						}
					}
				}
			}

			// 更新偏光片信息 下POL料号和对应上POL料号
			updatePartPOL(map);

		} catch (ChangeException2 e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (list.contains("Y")) {
				iflegislationAudit = "Y";
			}
		}
		return iflegislationAudit;
	}

	/**
	 * 更新 上下POL料号
	 * 
	 * @param map
	 */
	public static void updatePartPOL(Map<String, WTPart> map) {
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next().toString();
			/*
			 * //上偏光片 if (key.indexOf(softype_TopPolarizer)!=-1) {
			 * 
			 * }else if(key.indexOf(softype_BotPolarizer)!=-1){ // 下偏光片
			 * 
			 * }else if(key.indexOf(softype_PhantomPolarizer)!=-1){ // 虚拟偏光片组件
			 * 
			 * }
			 */
			if ("上偏光片".equals(key)) {
				WTPart part = map.get(key);
				if (map.containsKey("下偏光片")) {
					WTPart botpart = map.get("下偏光片");
					if (botpart != null) {
						try {
							// MtlNumofPOLBtn 对应下POL料号
							LWCUtil.setStringIBAValue(part, "MtlNumofPOLBtn", botpart.getNumber());
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			} else if ("下偏光片".equals(key)) {
				WTPart part = map.get(key);
				if (map.containsKey("上偏光片")) {
					WTPart toppart = map.get("上偏光片");
					if (toppart != null) {
						try {
							// MtlNumofPOLTop 对应上POL料号
							LWCUtil.setStringIBAValue(part, "MtlNumofPOLTop", toppart.getNumber());
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			} else if ("虚拟偏光片组件".equals(key)) {
				WTPart part = map.get(key);
				if (map.containsKey("上偏光片")) {
					WTPart toppart = map.get("上偏光片");
					if (toppart != null) {
						try {
							// MtlNumofPOLTop 对应上POL料号
							CSCIBA.setIBAStringValue(part, "MtlNumofPOLTop", toppart.getNumber());
						} catch (WTException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				if (map.containsKey("下偏光片")) {
					WTPart botpart = map.get("下偏光片");
					if (botpart != null) {
						try {
							// MtlNumofPOLBtn 对应下POL料号
							CSCIBA.setIBAStringValue(part, "MtlNumofPOLBtn", botpart.getNumber());
						} catch (WTException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}

	}
	
	/**
	 * 更新 上下POL料号
	 * 
	 * @param map
	 */
	public static void updatePartMark(Map<String, WTPart> map) {
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next().toString();
			if ("下偏光片".equals(key)) {
				WTPart part = map.get(key);
				if (map.containsKey("上偏光片")) {
					WTPart toppart = map.get("上偏光片");
					if (toppart != null) {
						try {
							// 取上偏光片Mark值
							String mark = IbaAttributeUtil.getAttributeValue(toppart,
									"partMark");
							System.out.println("=====POLPart mark:"+mark);
							//LWCUtil.setStringIBAValue(part, "MtlNumofPOLTop", toppart.getNumber());
							IBAUtil.forceSetIBAValue(part, "partMark", mark);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			} 
		}

	}


	/**
	 * 半成品正式编号
	 * 
	 * @param eco
	 * @param number
	 *            半成品编号集合
	 * @return
	 */
	public static String setPartNumberByECN(WTChangeOrder2 eco, String number) {
		log.info("=====setPartNumberByECN number:" + number);
		JSONObject object = new JSONObject();
		if (!isNullOrEmpty(number)) {
			try {
				object = new JSONObject(number);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			log.info("=====setPartNumberByECN object:" + object);
		}
		String iflegislationAudit = "";
		List<String> list = new ArrayList<String>();
		QueryResult queryResultECA;
		try {
			queryResultECA = ChangeHelper2.service.getChangeActivities(eco);
			if (queryResultECA != null) {
				while (queryResultECA.hasMoreElements()) {
					WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
					if (eca != null) {
						// 受影响对象
						QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
						if (queryResultPart != null) {
							while (queryResultPart.hasMoreElements()) {
								Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
								if (changeable != null && changeable instanceof WTPart) {
									WTPart part = (WTPart) changeable;
									String type = ext.tianma.bom.util.IBAUtil.getIBAValue(part, "SemiFinishedGoodType");
									// CheckPartReveal.setLegislationAudit(part);
									if (object.has(type)) {
										ext.tianma.part.PartCommands.setPartNumber(part, (String) object.get(type));
										// 设置分类属性
										setPartClassNode(object, part);
									}
								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (list.contains("Y")) {
				iflegislationAudit = "Y";
			}
		}
		return iflegislationAudit;
	}

	/**
	 * 如果半成品类型是CF则走采购信息审核节点
	 * 
	 * @param wtObj
	 * @return
	 */
	public static String opinionPartByECN(WTObject wtObj) {
		String ifsemiGoodAudit = "";

		List<String> list = new ArrayList<String>();
		if (wtObj != null && wtObj instanceof WTChangeOrder2) {
			WTChangeOrder2 ecn = (WTChangeOrder2) wtObj;
			try {
				QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(ecn);
				if (queryResultECA != null) {
					while (queryResultECA.hasMoreElements()) {
						WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
						if (eca != null) {
							// 受影响对象
							QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
							if (queryResultPart != null) {
								while (queryResultPart.hasMoreElements()) {
									Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
									if (changeable != null && changeable instanceof WTPart) {
										WTPart part = (WTPart) changeable;
										list.add(CheckPartReveal.opinionPart(part));
									}
								}
							}
						}
					}
				}
				if (list.contains("Y")) {
					ifsemiGoodAudit = "Y";
				}
			} catch (ChangeException2 e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return ifsemiGoodAudit;
	}

	/**
	 * 设置IBA 价格
	 * 
	 * @param wtObj
	 * @return
	 */
	public static String setSemiGoodsAuditInfoToIBAByECN(WTObject wtObj, String prcie) {
		String ifsemiGoodAudit = "";

		List<String> list = new ArrayList<String>();
		if (wtObj != null && wtObj instanceof WTChangeOrder2) {
			WTChangeOrder2 ecn = (WTChangeOrder2) wtObj;
			try {
				QueryResult queryResultECA = ChangeHelper2.service.getChangeActivities(ecn);
				if (queryResultECA != null) {
					while (queryResultECA.hasMoreElements()) {
						WTChangeActivity2 eca = (WTChangeActivity2) queryResultECA.nextElement(); // 获取ECA
						if (eca != null) {
							// 受影响对象
							QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(eca);
							if (queryResultPart != null) {
								while (queryResultPart.hasMoreElements()) {
									Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
									if (changeable != null && changeable instanceof WTPart) {
										WTPart part = (WTPart) changeable;
										ext.tianma.part.PartCommands.setSemiGoodsAuditInfoToIBA(prcie, part);
									}
								}
							}
						}
					}
				}
				if (list.contains("Y")) {
					ifsemiGoodAudit = "Y";
				}
			} catch (ChangeException2 e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return ifsemiGoodAudit;
	}

	public static String typeToProcess(WTObject primaryBusinessObject) {
		String result = "半成品";
		log.info("==========typeToProcess start===");
		log.info("====typeToProcess primaryBusinessObject =======" + primaryBusinessObject);
		WTChangeOrder2 eco = (wt.change2.WTChangeOrder2) primaryBusinessObject;
		wt.fc.QueryResult qureca;
		try {
			qureca = wt.change2.ChangeHelper2.service.getChangeActivities(eco);
			log.info("====typeToProcess qureca =======" + qureca.size());
			while (qureca.hasMoreElements()) {

				wt.change2.WTChangeActivity2 cha = (wt.change2.WTChangeActivity2) qureca.nextElement();
				log.info("====typeToProcess cha =======" + cha);

				if (cha != null) {
					// 受影响对象
					QueryResult queryResultPart = ChangeHelper2.service.getChangeablesBefore(cha);
					log.info("====typeToProcess queryResultPart =======" + queryResultPart);
					if (queryResultPart != null) {
						while (queryResultPart.hasMoreElements()) {
							Changeable2 changeable = (Changeable2) queryResultPart.nextElement();
							if (changeable != null && changeable instanceof wt.part.WTPart) {
								WTPart part = (WTPart) changeable;
								String softType = GenericUtil.getTypeId(part);
								log.info("===========typeToProcess  softType:" + softType);
								if (softType.indexOf("cn.tianma.SemiFinishedGood") != -1) {
									log.info("====TO SemiFinishedGood Process=======");
									result = "半成品";
									log.info("==========typeToProcess middle SemiFinishedGood===");
									return result;
								} else if (softType.indexOf("cn.tianma.RawMaterial") != -1) {
									log.info("====TO RawMaterial Process=======");
									result = "原材料";
									log.info("==========typeToProcess middle RawMaterial===");
									return result;
								}
								// RawMaterial
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.info("=========typeToProcess exception:" + e.getMessage());
			e.printStackTrace();
		}
		log.info("==========typeToProcess end result===" + result);
		return result;
	}

	/**
	 * 设置分类属性
	 * 
	 * @param object
	 * @param part
	 */
	public static void setPartClassNode(JSONObject object, WTPart part) {
		try {
			part = GenericUtil.getPart(part.getNumber(), "");
			String classtype = (String) LWCUtil.getValue(part, "SemiFinishedGoodType");
			if ("C1".equalsIgnoreCase(classtype)) {
				// 对应CF料号and
				// 对应LC料号and
				// 对应TFT料号
			} else if ("C2".equalsIgnoreCase(classtype)) {
				// 对应C1料号
				if (object.has("C1")) {
					CSCIBA.setIBAStringValue(part, "C1MtlNum", object.getString("C1"));
				}
			} else if ("C3".equalsIgnoreCase(classtype)) {
				// 对应C1 or C8料号,优先C1，无C1，对应C8
				if (object.has("C1")) {
					CSCIBA.setIBAStringValue(part, "C1MtlNum", object.getString("C1"));
				} else if (object.has("C8")) {
					CSCIBA.setIBAStringValue(part, "C1MtlNum", object.getString("C8"));
				}
				// 对应C1、C2料号
				if (object.has("C1")) {
					CSCIBA.setIBAStringValue(part, "MtlNumofC1C2", object.getString("C1"));
				}
			} else if ("C5".equalsIgnoreCase(classtype)) {
				// 对应C2料号 C2MtlNum
				if (object.has("C2")) {
					CSCIBA.setIBAStringValue(part, "C2MtlNum", object.getString("C2"));
				}

			} else if ("C6".equalsIgnoreCase(classtype)) {
				// 对应C5 or C7 or
				// TM料号,优先C7，无C7，对应C5，如果无C5或C7，申请人直接写入TM，则流程通过后，无需再写入C5或C7的值
				if (object.has("C7")) {
					CSCIBA.setIBAStringValue(part, "MtlNumofC5C7", object.getString("C7"));
				} else if (object.has("C5")) {
					CSCIBA.setIBAStringValue(part, "MtlNumofC5C7", object.getString("C5"));
				}
				// MtlNumofC5C7

			} else if ("C7".equalsIgnoreCase(classtype)) {
				// 对应C2 or C5 or C8 or
				// TM料号,优先C7，无C5，对应C8，如果无C8或C5，，对应C2，申请人直接写入TM，则流程通过后，无需再写入C2 或
				// C5 或 C8的值
				// C2C5MtlNum
				if (object.has("C5")) {
					CSCIBA.setIBAStringValue(part, "C2C5MtlNum", object.getString("C5"));
				} else if (object.has("C8")) {
					CSCIBA.setIBAStringValue(part, "C2C5MtlNum", object.getString("C8"));
				} else if (object.has("C2")) {
					CSCIBA.setIBAStringValue(part, "C2C5MtlNum", object.getString("C2"));
				}
				// C2C5MtlNum

			} else if ("C8".equalsIgnoreCase(classtype)) {
				// 对应C1料号（针对外购武汉F8时，请填写对应的CF料号、LC料号、TFT料号）
				// 对应C1料号
				if (object.has("C1")) {
					CSCIBA.setIBAStringValue(part, "C1MtlNum", object.getString("C1"));
				}
			} else if ("C9".equalsIgnoreCase(classtype)) {
				// 对应C1料号
				if (object.has("C1")) {
					CSCIBA.setIBAStringValue(part, "C1MtlNum", object.getString("C1"));
				}
			} else if ("L2".equalsIgnoreCase(classtype)) {
				// 对应L1料号 MtlNumofL1
				if (object.has("L1")) {
					CSCIBA.setIBAStringValue(part, "MtlNumofL1", object.getString("L1"));
				}
			} else if ("L4".equalsIgnoreCase(classtype)) {
				// 对应L3料号 NewMtlNumofL3
				if (object.has("L3")) {
					CSCIBA.setIBAStringValue(part, "NewMtlNumofL3", object.getString("L3"));
				}
			} else if ("S4".equalsIgnoreCase(classtype)) {
				// 对应S3料号 MtlNumofS3
				if (object.has("S3")) {
					CSCIBA.setIBAStringValue(part, "MtlNumofS3", object.getString("S3"));
				}
			} /*
				 * else if("E1".equalsIgnoreCase(classtype)){ //对应A4 or A5 or A6
				 * NewMtlNumofA4A5 if(object.has("A4")){
				 * CSCIBA.setIBAStringValue(part, "NewMtlNumofA4A5",
				 * object.getString("A4")); } }else
				 * if("E2".equalsIgnoreCase(classtype)){ //对应A4 or A5 or A6 and
				 * POL料号
				 * 
				 * }else if("E3".equalsIgnoreCase(classtype)){ //对应A1 or A2 or
				 * A3
				 * 
				 * }
				 */else if ("E4".equals(classtype)) {
				// 对应(A1 or A2 or A3 or E5) and POL,优先对应E5
				// NewMtlNumofA1A2A3E5POL
				if (object.has("E5")) {
					CSCIBA.setIBAStringValue(part, "NewMtlNumofA1A2A3E5POL", object.getString("E5"));
				}
			} /*
				 * else if("E5".equals(classtype)){ //对应A1 or A2 or A3
				 * 
				 * }
				 */else if ("E6".equals(classtype)) {
				// 对应E5 or E7 cn.tianma.MtlNumofA1A2A3E5
				if (object.has("E5")) {
					CSCIBA.setIBAStringValue(part, "NewMtlNumofA1A2A3E5POL", object.getString("E5"));
				} else if (object.has("E7")) {
					CSCIBA.setIBAStringValue(part, "NewMtlNumofA1A2A3E5POL", object.getString("E7"));
				}
			} else if ("E7".equals(classtype)) {
				// 对应E5 cn.tianma.MtlNumofA1A2A3E5
				if (object.has("E5")) {
					CSCIBA.setIBAStringValue(part, "NewMtlNumofA1A2A3E5POL", object.getString("E5"));
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static boolean updateRejectFlag(WTArrayList parts) {
		boolean flag = false;
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
	        PropertiesUtil ut = new PropertiesUtil(configFilePath);
			String url = ut.getValueByKey("TM_JDBC_URL");
			String user =  ut.getValueByKey("TM_JDBC_USER");
			String pwd =  ut.getValueByKey("TM_JDBC_PWD");
			//conn = DriverManager.getConnection("jdbc:oracle:thin:@10.100.6.191:1521:wind", "plm2erp", "plm2erp");
			conn = DriverManager.getConnection(url, user, pwd);
			int size = parts.size() ;
			for(int i = 0 ; i < size ; i++ ){
				Persistable persistable = parts.getPersistable(i) ;
				if( persistable != null && persistable instanceof WTPart ){
					WTPart part = ( WTPart ) persistable ;
					String SQL = "update pdm_erp_part set FLAG_SOURCING = 'N',SOURCINGREJECT_FLAG = '10' where item_id = ? and pview = ? ";
					int rs = 0;
					PreparedStatement pstmt = null;
					pstmt = conn.prepareStatement(SQL);
					pstmt.setString(1, part.getNumber());
					pstmt.setString(2, part.getViewName());
					rs = pstmt.executeUpdate();
					log.debug("=================rs==========" + rs);
					if (rs != 0) {
						flag = true;
					}
					pstmt.close();
					pstmt = null;
				}
			}	
			conn.close();
			conn = null;

		} catch (Exception e1) {
			// trans.rollback();
			flag = false;
			e1.printStackTrace();
			System.out.println("===========update failed=====" + e1);
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return flag;
	}
	public static void main(String[] args) {
		String numbers = "1,2,3,4";
		if (numbers.length() != 0 && numbers.lastIndexOf(",") == (numbers.length() - 1)) {
			numbers = numbers.substring(0, numbers.length() - 2);
		}
		// log.info(numbers);
	}
	public static String validateNumber(WTChangeOrder2 eco){
		String result = "";
		QueryResult qr;
		try {
			qr = WfEngineHelper.service.getAssociatedProcesses(eco, WfState.OPEN_RUNNING, null);
			WfProcess process = null;
			while (qr.hasMoreElements()) {
				process = (WfProcess) qr.nextElement();
				if (process.getTemplate().getName().equals(MaterialWorkflow)) {
					ProcessData processData = process.getContext();
					Object obj =  processData.getValue("正式编码");
					if(obj!=null){
						String number = (String)obj;
						if(isNullOrEmpty(number)){
							result = "正式编号不能为空";
						}
					}
				}
				break;

			}
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
}
